package com.kaikeba.wx.controller;

import com.kaikeba.bean.BootStrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseText;
import com.kaikeba.service.ExpressService;
import com.kaikeba.service.impl.ExpressServiceImpl;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class ExpressController {
    @ResponseText("/wx/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response) {
        User wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getPhone();
        ExpressService expressService = new ExpressServiceImpl();
        List<Express> list = expressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "未出库" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            String code = e.getCode() == null ? "已取件" : e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUsername(), e.getUserPhone(), e.getCompany(), code, inTime, outTime, status, e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if (list.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
            Stream<BootStrapTableExpress> status0Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("待取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1Time - o2Time);
            });
            Stream<BootStrapTableExpress> status1Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("已取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1Time - o2Time);
            });
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            Map data = new HashMap<>();
            data.put("status0", s0);
            data.put("status1", s1);
            msg.setData(data);
        }
        String json = JSONUtil.toJSON(msg.getData());
        return json;
    }

    //    @ResponseText("/wx/userExpressList.do")
//    public String expressList(HttpServletRequest request,HttpServletResponse response){
//        String userPhone = request.getParameter("userPhone");
//        List<Express> list = ExpressService.findByUserPhoneAndStatus(userPhone, 0);
//        List<BootStrapTableExpress> list2 = new ArrayList<>();
//        for(Express e:list){
//            String inTime = DateFormatUtil.format(e.getInTime());
//            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
//            String status = e.getStatus()==0?"待取件":"已取件";
//            String code = e.getCode()==null?"已取件":e.getCode();
//            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
//            list2.add(e2);
//        }
//        Message msg = new Message();
//        if(list.size() == 0){
//            msg.setStatus(-1);
//            msg.setResult("未查询到快递");
//        }else{
//            msg.setStatus(0);
//            msg.setResult("查询成功");
//            msg.setData(list2);
//        }
//        return JSONUtil.toJSON(msg);
//    }
    @ResponseText("/wx/lazyBoard.do")
    public String lazyBoard(HttpServletRequest request, HttpServletResponse response) {

        String selectString = request.getParameter("select");

        int select = selectString == null ? 0 : Integer.parseInt(selectString);

        ExpressService service = new ExpressServiceImpl();
        List<Map<String, String>> list = service.lazyBord(select);
        Message msg = new Message();
        if (list.size() > 0) {
            msg.setStatus(0);
            msg.setData(list);
        } else {
            msg.setStatus(-1);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseText("/wx/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        Express e = new Express(number, username, userPhone, company, UserUtil.getWxUser(request.getSession()).getPhone());
        ExpressService service = new ExpressServiceImpl();
        boolean insertResult = service.insert(e);
        Message message = new Message();
        if (insertResult){
            message.setStatus(0);
            message.setResult("添加成功！");
        }else {
            message.setStatus(-1);
            message.setResult("添加失败，请重试！");
        }


        return JSONUtil.toJSON(message);
    }
}
